<?php
class Admin_SettingController extends Zend_Controller_Action{
	public function init(){
		if( !At_User::getInstance()->hasIdentity() ){
			$this->_redirect('/admin/user/login');
			exit;
		}

		$this->_helper->layout->setLayout('admin');
	}

	public function indexAction(){
		$this->_forward('list');
	}

	public function listAction(){
		$setting = new Default_Model_Setting();
		$this->view->settings = $setting->fetchAll();
	}

	public function editAction(){
		$request = $this->getRequest();
		$form    = "Cannot edit this setting";

		$setting = new Default_Model_Setting( $request->id );
		//var_dump( $setting->name );


		$formName = sprintf('Admin_Form_Setting_%s', ucfirst( $setting->name ));

		if( class_exists( $formName ) ){
			$form = new $formName( $setting );
		}

		if( $form instanceof Zend_Form && $request->isPost() && $form->isValid( $request->getPost() ) ){
			if( $request->add ){
				$movie = new Default_Model_Movie( $request->new_movie );
				$values = unserialize( $setting->value );

				if( !$values ){
					$values = array();
				}


				$values[] = array('id' => $movie->id, 'title' => $movie->title );
				$setting->value = $values;
				$setting->save();
			}

		}


		$this->view->form    = $form;
		$this->view->setting = $setting;
	}





}
